﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nebular.ExpenseManagement.Infrastructure;

namespace Nebular.ExpenseManagement.Model
{
    [Serializable]
    public class Group : Principal, IGroup
    {
        readonly IEnumerable<IPrincipal> _members;

        public Group(Guid id, string name, IEnumerable<IPrincipal> members)
            : base(id, name)
        {
            _members = members;    
        }

        public Group(xpPrincipal group) : this(
                group.Id,
                group.Name,
                group.Members.Select(p => Principal.FromExpPrincipal(p)))
        {
            if (group.PrincipalType != "g")
                throw new ArgumentException("Principal must be of group type");
        }

        #region IGroup Members

        public IEnumerable<IPrincipal> Members
        {
            get { return _members; }
        }

        public IGroup AddMember(IPrincipal member)
        {
            throw new NotImplementedException();
        }

        #endregion

        internal static IEnumerable<IGroup> Convert(IEnumerable<xpPrincipal> groups)
        {
            foreach (var group in groups)
                yield return new Group(group);
        }

    }
}
